import { Injectable } from '@nestjs/common'
// import { UsersService } from '../users/users.service'
import { JwtService } from '@nestjs/jwt'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { User } from '../users/users.entity'

@Injectable()
export class AuthService {
  constructor(
    private readonly jwtService: JwtService,
    @InjectRepository(User) private userRepository: Repository<User>,
  ) {}

  async validateUser(tel: string, pass: string): Promise<any> {
    const user = await this.userRepository.findOne({ where: { tel } })
    if (user) {
      // && user.password === res?.code
      // const { password, ...result } = user
      return user
    }
    return null
  }

  async login(user: any) {
    console.log('user', user)

    const payload = { username: user.username, sub: user.userId }
    return {
      access_token: this.jwtService.sign(payload),
    }
  }
}
